package com.longrise.android.byjk.plugins.course.mycourse;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.AllCourseChildBean;
import com.longrise.android.byjk.plugins.course.coursedetail.CourseDetailActivity2;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.common.base.BaseFragment;

import java.util.List;

/**
 * Created by callxkj on 2017/10/25.
 * 我的课程
 */

public class MyCourseDetailFragment extends BaseFragment<MyCourseDetailPresenter>
        implements SwipeRefreshLayout.OnRefreshListener, MyCourseDetailAdapter.OnItemClickListener, MyCourseDetailContract.View {

    private static final String TAG = "MyCourseDetailFragment";
    private BBswipeRefreshLayout mSrl;
    private RecyclerView mRcv;
    public static final String TRAINTYPE = "mTraintype";
    private String mTraintype;
    private MyCourseDetailAdapter mAdapter;

    @Override
    protected int getLayoutResourse(@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_mycoursedetail;
    }

    @Override
    public void initView() {
        mSrl = (BBswipeRefreshLayout) mRootView.findViewById(R.id.mycoursedetail_fragment_swipelayout);
        mRcv = (RecyclerView) mRootView.findViewById(R.id.mycoursedetail_fragment_irv);

        Bundle arguments = getArguments();
        mTraintype = arguments.getString(TRAINTYPE);
        initRcv();
        initEvent();
        mPresenter.getData(mTraintype);
    }

    private void initRcv() {
        View headView = View.inflate(mContext, R.layout.item_seconddetailfragment_headview, null);
        mAdapter = new MyCourseDetailAdapter();
        mAdapter.addHeaderView(headView);
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRcv.setLayoutManager(manager);
        mRcv.setAdapter(mAdapter);
    }

    private void initEvent() {
        mSrl.setOnRefreshListener(this);
        mAdapter.setOnItemClickListener(this);
    }


    @Override
    public void onRefresh() {
       mPresenter.getData(mTraintype);
    }

    @Override
    public void onClick(AllCourseChildBean item) {
        String courseid = item.getCourseid();
        Intent courseDetailIntent = new Intent(mContext, CourseDetailActivity2.class);
        courseDetailIntent.putExtra(CourseDetailActivity2.INTENT_EXTRA_PARAMS, courseid);  //courseid  groupid
        startActivity(courseDetailIntent);
    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    @Override
    public void refreshData(List<AllCourseChildBean> childBeanList) {
        mAdapter.refreshData(childBeanList);
    }

    @Override
    public void setRefreshing(boolean isRefreshing) {
        mSrl.setRefreshing(isRefreshing);
    }

}
